function RTree() {
	this.setFirstBranchExpandedImage(rjsPath+"/RTree1be.gif");
	this.setFirstBranchCollapsedImage(rjsPath+"/RTree1bc.gif");
	this.setMiddleBranchExpandedImage(rjsPath+"/RTree2be.gif");
	this.setMiddleBranchCollapsedImage(rjsPath+"/RTree2bc.gif");
	this.setLastBranchExpandedImage(rjsPath+"/RTree3be.gif");
	this.setLastBranchCollapsedImage(rjsPath+"/RTree3bc.gif");
	this.setSingleBranchExpandedImage(rjsPath+"/RTree0be.gif");
	this.setSingleBranchCollapsedImage(rjsPath+"/RTree0bc.gif");
	this.setFirstLeafImage(rjsPath+"/RTree1l.gif");
	this.setMiddleLeafImage(rjsPath+"/RTree2l.gif");
	this.setLastLeafImage(rjsPath+"/RTree3l.gif");
	this.setSingleLeafImage(rjsPath+"/RTree0l.gif");
	this.setIndentImage(rjsPath+"/RTreeIndent.gif");
	this.setContextMenuImage("");
	this.children=new Array();
	this.setDepth(0);
	this.setSelectedNode(null);
}
RTree.prototype={
	getUI:function(){if(!isDefined(this.ui))return null; else return this.ui;},
	setFirstBranchExpandedImage: function(image){this.firstBranchExpandedImage=image;},
	getFirstBranchExpandedImage: function(){return this.firstBranchExpandedImage;},
	setFirstBranchCollapsedImage: function(image){this.firstBranchCollapsedImage=image;},
	getFirstBranchCollapsedImage: function(){return this.firstBranchCollapsedImage;},
	setMiddleBranchExpandedImage: function(image){this.middleBranchExpandedImage=image;},
	getMiddleBranchExpandedImage: function(){return this.middleBranchExpandedImage;},
	setMiddleBranchCollapsedImage: function(image){this.middleBranchCollapsedImage=image;},
	getMiddleBranchCollapsedImage: function(){return this.middleBranchCollapsedImage;},
	setLastBranchExpandedImage: function(image){this.lastBranchExpandedImage=image;},
	getLastBranchExpandedImage: function(){return this.lastBranchExpandedImage;},
	setLastBranchCollapsedImage: function(image){this.lastBranchCollapsedImage=image;},
	getLastBranchCollapsedImage: function(){return this.lastBranchCollapsedImage;},
	setSingleBranchExpandedImage: function(image){this.singleBranchExpandedImage=image;},
	getSingleBranchExpandedImage: function(){return this.singleBranchExpandedImage;},
	setSingleBranchCollapsedImage: function(image){this.singleBranchCollapsedImage=image;},
	getSingleBranchCollapsedImage: function(){return this.singleBranchCollapsedImage;},
	setFirstLeafImage: function(image){this.firstLeafImage=image;},
	getFirstLeafImage: function(){return this.firstLeafImage;},
	setMiddleLeafImage: function(image){this.middleLeafImage=image;},
	getMiddleLeafImage: function(){return this.middleLeafImage;},
	setLastLeafImage: function(image){this.lastLeafImage=image;},
	getLastLeafImage: function(){return this.lastLeafImage;},
	setSingleLeafImage: function(image){this.singleLeafImage=image;},
	getSingleLeafImage: function(){return this.singleLeafImage;},
	setIndentImage:function(image){this.indentImage=image;},
	getIndentImage:function(){return this.indentImage;},
	setContextMenuImage:function(image) {this.contextMenuImage = image;},
	getContextMenuImage:function(){return this.contextMenuImage;},
	getChildren:function(){return this.children;},
	setDepth:function(depth){this.depth=depth;},
	getDepth:function(){return this.depth;},
	setSelectedNode:function(node){this.selectedNode=node;},
	getSelectedNode:function(){return this.selectedNode;},
	createUI:function(){
		var div=document.createElement("div");
		this.ui=div;
		this.ui.model = this;
		this.ui.className="RTreeDiv";
		for (var i=0; i<this.getChildren().length;++i) this.getChildren()[i].createUI();
		return div;
	},
	addChild:function(node){
		this.children[this.children.length]=node;
		node.setParent(this);
		node.setTree(this);
		node.setDepth(0);
	},
	showIn:function(div) {div.insertBefore(this.createUI());},
	setData:function(data) {
		// data=[{caption, onFetch, onSelect,  ei, ci, li, [children]}]
		// ei-Expanded Image, ci-Collapsed Image, li-Leaf Image
		for (var i=0; i<data.length; ++i) {
			var node=new RTreeNode();
			node.setUuid(data[i].uuid);
			node.setCaption(data[i].caption);
			node.setObject(data[i].object);
			node.setOnFetch(data[i].onFetch);
			node.setOnSelect(data[i].onSelect);
			if (data[i].ei != null&&data[i].ei != "")node.setExpandedImage(data[i].ei);
			if (data[i].ci != null&&data[i].ci != "")node.setExpandedImage(data[i].ci);
			if (data[i].li != null&&data[i].li != "")node.setExpandedImage(data[i].li);
			this.addChild(node);
			var children = data[i].children;
			if (children != null) node.setData(children);
		}
	}
}

function RTreeNode() {
	this.setTree(null);
	this.setParent(null);
	this.setCollapsedImage(rjsPath+"/RTreeNodeC.gif");
	this.setExpandedImage(rjsPath+"/RTreeNodeE.gif");
	this.setLeafImage(rjsPath+"/RTreeNodeL.gif");
	this.setObject(null);
	this.setUuid(null);
	this.setCaption(null);
	this.setAction(null);
	this.setContextMenu(null);
	this.setOnSelect(null);
	this.setOnExpand(null);
	this.setOnCollapse(null);
	this.setOnContextMenu(null);
	this.setOnAction(null);
	this.setIsExpanded(false);
	this.children=new Array();
	this.setDepth(0);
	this.setOnFetch(null);
}
RTreeNode.prototype = {
	getUI:function(){if(!isDefined(this.ui))return null; else return this.ui;},
	setTree:function(tree){this.tree=tree;},
	getTree:function(){return this.tree;},
	setParent:function(node){this.parent=node;},
	getParent:function(){return this.parent;},
	setLeafImage:function(image){this.leafImage = image;},
	getLeafImage:function(){return this.leafImage;},
	setExpandedImage:function(image){this.expandedImage = image;},
	getExpandedImage:function(){return this.expandedImage;},
	setCollapsedImage:function(image){this.collapsedImage = image;},
	getCollapsedImage:function(){return this.collapsedImage;},
	setObject:function(object){this.object = object;},
	getObject:function(){return this.object;},
	setCaption:function(caption){this.caption = caption;},
	getCaption:function(){return this.caption;},
	setUuid: function(uuid) { this.uuid = uuid; },
	getUuid: function() { return this.uuid; },
	setAction:function(action){this.action = action;},
	getAction:function(){return this.action;},
	setContextMenu:function(contextMenu){this.contextMenu = contextMenu;},
	getContextMenu:function(){return this.contextMenu;},
	getChildren:function(){return this.children;},
	setOnSelect:function(func){this.onSelect=func;},
	getOnSelect:function(){return this.onSelect;},
	setOnExpand:function(func){this.onExpand=func;},
	getOnExpand:function(){return this.onExpand;},
	setOnCollapse:function(func){this.onCollapse=func;},
	getOnCollapse:function(){return this.onCollapse;},
	setOnContextMenu:function(func){this.onContextMenu=func;},
	getOnContextMenu:function(){return this.onContextMenu;},
	setOnAction:function(func){this.onAction=func;},
	getOnAction:function(){return this.onAction;},
	setIsExpanded:function(b){this.isExpand=b;},
	getIsExpanded:function(){return this.isExpand;},
	setDepth:function(depth){this.depth=depth;},
	getDepth:function(){return this.depth;},
	getHasChildren:function(){return this.getChildren().length>0;},
	getIsFirstChild:function(){return this.getParent().getChildren()[0]==this;},
	getIsLastChild:function(){var children=this.getParent().getChildren(); return children[children.length-1]==this;},
	getIndex:function(){
		var siblings = this.getParent().getChildren();
		for (var i=0; i<siblings.length; ++i)
			if (siblings[i]==this) return i;
	},
	getPreviousSibling:function(){
		var siblings = this.getParent().getChildren();
		var i = this.getIndex() - 1;
		if (i >= 0) return siblings[i];
		else return null;
	},
	getNextSibling:function(){
		var siblings = this.getParent().getChildren();
		var i = this.getIndex() + 1;
		if (i < siblings.length) return siblings[i];
		else return null;
	},
	getConnectorImage:function(){
		var parent = this.getParent();
		var tree = this.getTree();
		if (this.getHasChildren() || (this.onFetch != null && this.onFetch != "")) {
			if (parent==tree && parent.getChildren().length==1) // single root branch
				return this.getIsExpanded() ? tree.getSingleBranchExpandedImage():tree.getSingleBranchCollapsedImage();
			else if (parent==tree && parent.getChildren()[0]==this) // first root branch
				return this.getIsExpanded() ? tree.getFirstBranchExpandedImage(): tree.getFirstBranchCollapsedImage();
			else if (parent.getChildren()[parent.getChildren().length-1]==this) // last branch
				return this.getIsExpanded() ? tree.getLastBranchExpandedImage(): tree.getLastBranchCollapsedImage();
			else // middle branch 
				return this.getIsExpanded() ? tree.getMiddleBranchExpandedImage(): tree.getMiddleBranchCollapsedImage();
		} else {
			if (parent==tree && parent.getChildren().length==1) // single root leaf
				return tree.getSingleLeafImage();
			else if (parent==tree && parent.getChildren()[0]==this) // first root leaf 
				return tree.getFirstLeafImage();
			else if (parent.getChildren()[parent.getChildren().length-1]==this) // last leaf
				return tree.getLastLeafImage();
			else // middle leaf 
				return tree.getMiddleLeafImage();
		}
	},
	addChild:function(node) {
		this.children[this.children.length] = node;
		node.setParent(this);
		node.setTree(this.getTree());
		node.setDepth(this.getDepth()+1);
		if (this.getTree().getDepth() < node.getDepth()) this.getTree().setDepth(node.getDepth());
	},
	select:function() {
		var f=this.getOnSelect(); if (f!=null&&f!="") eval(f)(this);
		// this.expand();
		var sn = this.getTree().getSelectedNode(); if (sn != null) sn.span.className="RTreeNode";
		this.span.className="RTreeNodeSelected";
		this.getTree().setSelectedNode(this);
		return;
		// make this visible
		var sn = this.getParent();
		var p = sn;
		while (sn != this.getTree()) {
			if (!sn.getIsExpanded()) {sn.setIsExpanded(true); p = sn;}
			sn=sn.getParent();
		}
		if (p!=this.getTree()) p.updateUI();
	},
	expand:function() {
		if (this.getIsExpanded()) return;
		var f=this.getOnFetch(); if (f!=null&&f!="") eval(f)(this); this.setOnFetch(null);
		var f = this.getOnExpand();	if (f!=null&&f!="") eval(f)(this);
		this.setIsExpanded(true);
		this.connector.src=this.getConnectorImage();
		for (var i=0; i<this.getChildren().length; ++i)
			this.getChildren()[i].getUI().style.display="block";
	},
	collapse:function() {
		if (!this.getIsExpanded()) return;
		var f = this.getOnCollapse(); if (f!=null&&f!="") eval(f)(this);
		this.setIsExpanded(false);
		this.connector.src=this.getConnectorImage();
		for (var i=0; i<this.getChildren().length; ++i)
			this.getChildren()[i].getUI().style.display="none";
	},
	createUI:function(){
		var o = this.getUI();
		var div=document.createElement("div");
		div.className = "RTreeNode";
		if (o==null) this.getParent().getUI().insertBefore(div);
		else this.getParent().getUI().replaceChild(div, o);
		this.ui=div;
		this.ui.model=this;
		if (this.getParent()!=this.getTree()) {
			if (this.getParent().getIsExpanded()) div.style.display="block";
			else div.style.display="none";
		} else div.style.display="block";
		var nextElement = null;
		var inode = this.parent;
		for (var i=this.getDepth()-1; i>=0; --i) {
			var img = document.createElement("img");
			img.className="RTreeImg";
			if (inode.getIsLastChild()) img.src=rjsPath+"/RBlank.gif";
			else img.src=this.getTree().getIndentImage();
			div.insertBefore(img, nextElement);
			nextElement = img;
			inode = inode.getParent();
		}
		var img = document.createElement("img");
		img.className="RTreeImg";
		img.src=this.getConnectorImage();
		img.onclick=this.clickConnector;
		this.connector=img;
		img.model=this;
		div.insertBefore(img);
		var img = document.createElement("img");
		img.className="RTreeImg";
		if (this.getHasChildren()) {
			if (this.getIsExpanded()) img.src=this.getExpandedImage();
			else img.src=this.getCollapsedImage();
		} else if (this.getOnFetch() != null && this.getOnFetch()!="") {
			img.src=this.getCollapsedImage();
		} else img.src=this.getLeafImage();
		div.insertBefore(img);
		var span=document.createElement("span");
		span.innerText=this.getCaption(); 
		span.onclick=this.clickCaption;
		span.className="RTreeNode";
		this.span=span;
		span.model = this;
		div.insertBefore(span);
		var children = this.getChildren();
		for (var i=0;i<children.length;++i)	children[i].createUI();
	},
	setOnFetch:function(f){this.onFetch=f;},
	getOnFetch:function(){return this.onFetch;},
	clickCaption:function(){
		this.model.select();
	},
	clickConnector:function(){
		if (this.model.getIsExpanded())	this.model.collapse();
		else this.model.expand();
	},
	setData:function(data) {
		// data=[{caption, onFetch, onSelect,  ei, ci, li, [children]}]
		// ei-Expanded Image, ci-Collapsed Image, li-Leaf Image
		for (var i=0; i<data.length; ++i) {
			var node=new RTreeNode();
			node.setUuid(data[i].uuid);
			node.setCaption(data[i].caption);
			node.setObject(data[i].object);
			node.setOnFetch(data[i].onFetch);
			node.setOnSelect(data[i].onSelect);
			if (data[i].ei != null&&data[i].ei != "")node.setExpandedImage(data[i].ei);
			if (data[i].ci != null&&data[i].ci != "")node.setExpandedImage(data[i].ci);
			if (data[i].li != null&&data[i].li != "")node.setExpandedImage(data[i].li);
			this.addChild(node);
			var children = data[i].children;
			if (children != null) node.setData(children);
		}
	},
	getPath:function(){
		var s = this.getCaption();
		var node = this.getParent();
		while (node != this.getTree()) {
			s = node.getCaption() + "/" + s;
			node = node.getParent();
		}
		return "/" + s;
	}
}

